package com.wy.sloth.receiver.controller;

import javax.annotation.Resource;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson.JSONObject;
import com.wy.sloth.model.SlothPage;
import com.wy.sloth.receiver.model.R;
import com.wy.sloth.redis.service.SlothRedisService;

/**
 * 消息日志控制器演示示例
 * 
 * @author wangyuan
 * @since 2019年3月6日
 */
@RestController
@RequestMapping(value = "/sloth/log")
public class SlothLogController {

    @Resource(name="redisTemplate")
    private RedisTemplate<String, JSONObject> redisTemplate;
    
    @Resource(name="redisTemplate")
    private RedisTemplate<String, String> stringRedisTemplate;
    
    @Autowired
    private SlothRedisService slothRedisService;
    
    /**
     * 响应最新创建的消息
     * @return
     */
    @RequestMapping(value = "/list", method = RequestMethod.POST)
    public R list() {
        SlothPage page = new SlothPage();
        page.setOrderByField("updatedTime");
        page.setSize(20);
        return R.ok().putData(slothRedisService.listMsgLog(page));
    }
    
    /**
     * 响应目前各个状态拥有的消息数量
     * @return
     */
    @RequestMapping(value = "/countStatus", method = RequestMethod.POST)
    public R countStatus() {
        return R.ok().putData(slothRedisService.countMsgStatus());
    }
    
    /**
     * 根据id获取消息详细信息
     * @param id
     * @return
     */
    @RequestMapping(value = "/dtl/{id}", method = RequestMethod.POST)
    public R getMsgLogDtl(@PathVariable(name = "id")String id) {
        return R.ok().putData(slothRedisService.getMsgLogDtlById(id));
    }
    
    /**
     * 根据状态获取消息列表
     * @param status
     * @return
     */
    @RequestMapping(value = "/list/{status}", method = RequestMethod.POST)
    public R listMsgLogByStatus(@PathVariable(name = "status")Integer status) {
        SlothPage page = new SlothPage();
        page.setSize(Integer.MAX_VALUE);
        return R.ok().putData(slothRedisService.listMsgLogByStatus(page, Integer.valueOf(status)));
    }
    
    /**
     * 清理所有的日志信息
     * @return
     */
    @RequestMapping(value = "/clearAll", method = RequestMethod.POST)
    public R clearAllMsgLog() {
    	slothRedisService.clearEarlyMsgLog(System.currentTimeMillis());
        return R.ok("清理成功");
    }
    
    /**
     * 重新处理消息
     * @param msgId
     * @param queueName
     * @return
     */
    @RequestMapping(value = "/resend", method = RequestMethod.POST)
    public R resend(String msgId, String queueName) {
    	throw new UnsupportedOperationException("暂不支持");
    }
    
    /**
     * 获取队列对应的消息type
     * @return
     */
    @RequestMapping(value = "/queue2Type/list", method = RequestMethod.POST)
    public R listQueue2Type() {
        return R.ok().putData(slothRedisService.listQueue2Type());
    }
    
    /**
     * 获取目前处理消息的消息队列
     * @return
     */
    @RequestMapping(value = "/queue/list", method = RequestMethod.POST)
    public R listQueue() {
        return R.ok().putData(slothRedisService.listQueueName());
    }
    
    /**
     * 清理消息队列
     * @return
     */
    @RequestMapping(value = "/clearQueue", method = RequestMethod.POST)
    public R clearQueue(String queueName) {
    	slothRedisService.clearMsgQueue(queueName);
        return R.ok();
    }
}
